{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Adding Spaces to a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: addSpaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #向字符串添加空格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ，以及一个下标从 <strong>0</strong> 开始的整数数组 <code>spaces</code> 。</p>\n",
    "\n",
    "<p>数组 <code>spaces</code> 描述原字符串中需要添加空格的下标。每个空格都应该插入到给定索引处的字符值 <strong>之前</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>s = \"EnjoyYourCoffee\"</code> 且 <code>spaces = [5, 9]</code> ，那么我们需要在 <code>'Y'</code> 和 <code>'C'</code> 之前添加空格，这两个字符分别位于下标 <code>5</code> 和下标 <code>9</code> 。因此，最终得到 <code>\"Enjoy <em><strong>Y</strong></em>our <em><strong>C</strong></em>offee\"</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你添加空格，并返回修改后的字符串<em>。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"LeetcodeHelpsMeLearn\", spaces = [8,13,15]\n",
    "<strong>输出：</strong>\"Leetcode Helps Me Learn\"\n",
    "<strong>解释：</strong>\n",
    "下标 8、13 和 15 对应 \"Leetcode<em><strong>H</strong></em>elps<em><strong>M</strong></em>e<em><strong>L</strong></em>earn\" 中加粗斜体字符。\n",
    "接着在这些字符前添加空格。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"icodeinpython\", spaces = [1,5,7,9]\n",
    "<strong>输出：</strong>\"i code in py thon\"\n",
    "<strong>解释：</strong>\n",
    "下标 1、5、7 和 9 对应 \"i<em><strong>c</strong></em>ode<em><strong>i</strong></em>n<em><strong>p</strong></em>y<em><strong>t</strong></em>hon\" 中加粗斜体字符。\n",
    "接着在这些字符前添加空格。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"spacing\", spaces = [0,1,2,3,4,5,6]\n",
    "<strong>输出：</strong>\" s p a c i n g\"\n",
    "<strong>解释：</strong>\n",
    "字符串的第一个字符前可以添加空格。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code> 仅由大小写英文字母组成</li>\n",
    "\t<li><code>1 &lt;= spaces.length &lt;= 3 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= spaces[i] &lt;= s.length - 1</code></li>\n",
    "\t<li><code>spaces</code> 中的所有值 <strong>严格递增</strong></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [adding-spaces-to-a-string](https://leetcode.cn/problems/adding-spaces-to-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [adding-spaces-to-a-string](https://leetcode.cn/problems/adding-spaces-to-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"LeetcodeHelpsMeLearn\"\\n[8,13,15]', '\"icodeinpython\"\\n[1,5,7,9]', '\"spacing\"\\n[0,1,2,3,4,5,6]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        j = 0\n",
    "        ans = []\n",
    "\n",
    "        pre = 0\n",
    "        for i in range(len(s)):\n",
    "            if i == spaces[j]:\n",
    "                ans.append(s[pre:i])\n",
    "                pre = i \n",
    "                j += 1\n",
    "\n",
    "                if j == len(spaces):\n",
    "                    ans.append(s[pre:])\n",
    "                    break\n",
    "\n",
    "\n",
    "\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        return ' '.join(s[i: j] for i, j in zip([0] + spaces, spaces + [len(s)]))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n = len(s)\n",
    "        spaces = [0] + spaces + [n]\n",
    "        strs = []\n",
    "        for i in range(len(spaces) - 1):\n",
    "            strs.append(s[spaces[i]: spaces[i + 1]])\n",
    "            if i != len(spaces) - 2:\n",
    "                strs.append(\" \")\n",
    "        return \"\".join(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        split_s = \"\"\n",
    "        index = 0\n",
    "        if len(spaces) == 1:\n",
    "            if spaces[index] == 0:\n",
    "                return \" \" + s\n",
    "            elif spaces[index] == 1:\n",
    "                return s + \" \"\n",
    "        for i in range(len(spaces)+1):\n",
    "            if index == spaces[-1]:\n",
    "                split_s += s[index:]\n",
    "                return split_s\n",
    "            else:\n",
    "                split_s += s[index:spaces[i]] + \" \"\n",
    "                index = spaces[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        #1.切片操作（超时）\n",
    "        #spaces_rev=sorted(spaces,reverse=True)\n",
    "        #for i in spaces_rev:\n",
    "        #    s=s[:i]+\" \"+s[i:]\n",
    "        #return s\n",
    "        #2.\n",
    "        ans=\"\"\n",
    "        count=0\n",
    "        n=len(s)\n",
    "        spaces.append(0)\n",
    "        if spaces[0]==0:\n",
    "            ans=ans+\" \"\n",
    "            count+=1\n",
    "        for i in range(n):\n",
    "            ans=ans+s[i]\n",
    "            i+=1\n",
    "            if i==spaces[count]:\n",
    "                count+=1\n",
    "                ans=ans+\" \"\n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n = len(s)\n",
    "        ret = ''\n",
    "        l = 0\n",
    "        spaces.append(10 ** 8)\n",
    "        for i in range(n):\n",
    "            if i == spaces[l]:\n",
    "                l += 1\n",
    "                ret += ' '\n",
    "            ret += s[i]\n",
    "            \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        split_s = \"\"\n",
    "        index = 0\n",
    "        if len(spaces) == 1:\n",
    "            if spaces[0] == 0:\n",
    "                return \" \" + s\n",
    "            elif spaces[0] == 1:\n",
    "                return s + \" \"\n",
    "        for i in range(len(spaces)+1):\n",
    "            if index == spaces[-1]:\n",
    "                split_s += s[index:]\n",
    "                return split_s\n",
    "            else:\n",
    "                split_s += s[index:spaces[i]] + \" \"\n",
    "                index = spaces[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = ''\n",
    "        k = 0\n",
    "        for num in spaces:\n",
    "            while k < num:\n",
    "                ans += s[k]\n",
    "                k += 1\n",
    "            ans += ' '\n",
    "        return ans + s[spaces[-1]:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        j=0\n",
    "        for i in spaces:\n",
    "            res+=s[j:i]+' '\n",
    "            j = i\n",
    "        res+=s[i:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        e=\"\"\n",
    "        j=0\n",
    "        i=0\n",
    "        for x in s:\n",
    "           if j<len(spaces) and i==spaces[j]:\n",
    "              e+=\" \"\n",
    "              j+=1  \n",
    "           i+=1     \n",
    "           e+=x\n",
    "        return e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        k  = 0\n",
    "        ls = []        \n",
    "        for i in spaces:\n",
    "            s1 =s[k:i] \n",
    "            k = i\n",
    "            ls.append(s1)\n",
    "        if spaces[-1] < len(s):\n",
    "            ls.append(s[spaces[-1]:])\n",
    "        return ' '.join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        # res = ''\n",
    "        # j = 0\n",
    "        # for i in spaces:\n",
    "        #     while j<len(s):\n",
    "        #         if j == i:\n",
    "        #             res+=' '\n",
    "        #             j = i\n",
    "        #             break\n",
    "        #         res += s[j]\n",
    "        #         j+=1\n",
    "        # while j<len(s):\n",
    "        #     res += s[j]\n",
    "        #     j+=1\n",
    "        # return res\n",
    "        res = ''\n",
    "        j=0\n",
    "        for i in spaces:\n",
    "            res+=s[j:i]+' '\n",
    "            j = i\n",
    "        res+=s[i:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        pre = 0\n",
    "        index = 0\n",
    "        res = []\n",
    "        for i in range(len(s)):\n",
    "            if index < len(spaces) and i == spaces[index]:\n",
    "                res.append(s[pre:i])\n",
    "                pre = i\n",
    "                index += 1\n",
    "        res.append(s[pre:])\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces.sort()\n",
    "        if spaces[0] == 0:\n",
    "            res = ' '\n",
    "            spaces = spaces[1:]\n",
    "        else:\n",
    "            res = ''\n",
    "        spaces.append(len(s))\n",
    "        spaces.append(0)\n",
    "        spaces.sort()\n",
    "        for i in range(len(spaces)-1):\n",
    "            res += s[spaces[i]:spaces[i+1]]\n",
    "            res += ' '\n",
    "        return res[:-1]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n = len(s)\n",
    "        c = -1\n",
    "        si = 0\n",
    "        sn = len(spaces)\n",
    "        r = []\n",
    "        for i in range(n):\n",
    "            c += 1\n",
    "            if si < sn and c == spaces[si]:\n",
    "                r.append(\" \")\n",
    "                si += 1\n",
    "            r.append(s[i])\n",
    "        return \"\".join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = []\n",
    "        ptr = 0\n",
    "        for k, v in enumerate(s):\n",
    "            if ptr < len(spaces) and spaces[ptr]==k:\n",
    "                ans.append(' ')\n",
    "                ptr += 1\n",
    "            ans.append(v)\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        j=0\n",
    "        for i in spaces:\n",
    "            res+=s[j:i]+' '\n",
    "            j = i\n",
    "        res+=s[i:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ret = \"\"\n",
    "        j, n = 0, len(spaces)\n",
    "        for i in range(len(s)):\n",
    "            if j < n and spaces[j] == i:\n",
    "                ret += \" \"\n",
    "                j += 1\n",
    "            ret += s[i]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        split_s = \"\"\n",
    "        sep = \" \"\n",
    "        index = 0\n",
    "        if len(spaces) == 1:\n",
    "            if spaces[index] == 0:\n",
    "                return sep + s\n",
    "            elif spaces[index] == 1:\n",
    "                return s + sep\n",
    "        for i in range(len(spaces)+1):\n",
    "            if index == spaces[-1]:\n",
    "                split_s += s[index:]\n",
    "                return split_s\n",
    "            else:\n",
    "                split_s += s[index:spaces[i]] + sep\n",
    "                index = spaces[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: list[int]) -> str:\n",
    "        ans = []\n",
    "        pre = 0\n",
    "        for space in spaces:\n",
    "            ans.append(s[pre:space])\n",
    "            pre = space\n",
    "        ans.append(s[pre:])     \n",
    "        return \" \".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans=\"\"\n",
    "        p=0\n",
    "        for i,v in enumerate(s):\n",
    "            if p!=len(spaces) and spaces[p]==i:\n",
    "                ans+=\" \"\n",
    "                ans+=v\n",
    "                p+=1\n",
    "            else:\n",
    "                ans+=v\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = []\n",
    "        n = len(s)\n",
    "        idx = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if idx < len(spaces) and spaces[idx] == i:\n",
    "                ans.append(' ')\n",
    "                idx += 1\n",
    "            ans.append(c)\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = []\n",
    "        spaces =  [0] + sorted(spaces) + [len(s)]\n",
    "        for i in range(len(spaces)-1):\n",
    "            ans.append(s[spaces[i]:spaces[i+1]])\n",
    "        return ' '.join(ans)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "\n",
    "        def only(s, spaces):\n",
    "            split_s = \"\"\n",
    "            index = 1\n",
    "            if len(spaces) == 1:\n",
    "                return \" \" + s\n",
    "            else:\n",
    "                s = \" \" + s\n",
    "                for i in range(len(spaces)+1):\n",
    "                    if index-1 == spaces[-1]:\n",
    "                        split_s += s[index:]\n",
    "                        return split_s\n",
    "                    else:\n",
    "                        split_s += s[index:spaces[i]+1] + \" \"\n",
    "                        index = spaces[i] + 1\n",
    "                \n",
    "\n",
    "        def more(s, spaces):\n",
    "            split_s = \"\"\n",
    "            index = 0\n",
    "            for i in range(len(spaces)+1):\n",
    "                if index == spaces[-1]:\n",
    "                    split_s += s[index:]\n",
    "                    return split_s\n",
    "                else:\n",
    "                    split_s += s[index:spaces[i]] + \" \"\n",
    "                    index = spaces[i]\n",
    "\n",
    "        if spaces[0] == 0:\n",
    "            return only(s, spaces)\n",
    "        else:\n",
    "            return more(s, spaces)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = ''\n",
    "        space_idx = 0\n",
    "        for i in range(len(s)):\n",
    "            if space_idx < len(spaces) and spaces[space_idx] == i:\n",
    "                ans += ' '\n",
    "                space_idx += 1\n",
    "            ans += s[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        cur = 0\n",
    "        l = len(spaces)\n",
    "        ans = []\n",
    "        for i in range(len(s)):\n",
    "            if i == spaces[cur]:\n",
    "                ans.append(' ')\n",
    "                cur += 1\n",
    "                if cur == l:\n",
    "                    ans.append(s[i:])\n",
    "                    break\n",
    "            ans.append(s[i])\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        cur = 0\n",
    "        l = len(spaces)\n",
    "        ans = []\n",
    "        for i in range(len(s)):\n",
    "            if i == spaces[cur]:\n",
    "                ans.append(' ')\n",
    "                cur += 1\n",
    "                if cur == l:\n",
    "                    ans.append(s[i:])\n",
    "                    break\n",
    "            ans.append(s[i])\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "       return ' '.join([s[ : spaces[0]]] + [s[a : b] for a, b in pairwise(spaces)] + [s[spaces[-1] : ]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        last_idx = 0\n",
    "        for space_before_pos in spaces:\n",
    "            if space_before_pos == 0:\n",
    "                res += ' '\n",
    "            else:\n",
    "                res += s[last_idx:space_before_pos]\n",
    "                res += ' '\n",
    "                last_idx = space_before_pos\n",
    "        else:\n",
    "            res += s[last_idx:]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        cur = 0\n",
    "        l = len(spaces)\n",
    "        ans = []\n",
    "        for i, c in enumerate(s):\n",
    "            if i == spaces[cur]:\n",
    "                ans.append(' ')\n",
    "                cur += 1\n",
    "                if cur == l:\n",
    "                    ans.append(s[i:])\n",
    "                    break\n",
    "            ans.append(c)\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        j = 0\n",
    "        for i in spaces:\n",
    "            while j<len(s):\n",
    "                if j == i:\n",
    "                    res+=' '\n",
    "                    j = i\n",
    "                    break\n",
    "                res += s[j]\n",
    "                j+=1\n",
    "        while j<len(s):\n",
    "            res += s[j]\n",
    "            j+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = list()\n",
    "\n",
    "        ptr = 0\n",
    "        for i, ch in enumerate(s):\n",
    "            if ptr < len(spaces) and spaces[ptr] == i:\n",
    "                ans.append(\" \")\n",
    "                ptr += 1\n",
    "            ans.append(ch)\n",
    "        \n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        result = []\n",
    "        new_spaces = []\n",
    "        pre_space = 0\n",
    "        for space in spaces:\n",
    "            new_spaces.append(space - pre_space)\n",
    "            pre_space = space\n",
    "\n",
    "        for space in new_spaces:\n",
    "            result.append(s[0: space])\n",
    "            s = s[space:]\n",
    "            \n",
    "        result.append(s)\n",
    "\n",
    "        return ' '.join(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        strs = []\n",
    "        strs.append(s[0:spaces[0]])\n",
    "        n = len(spaces)\n",
    "        for i in range(n-1):\n",
    "            strs.append(s[spaces[i]:spaces[i+1]])\n",
    "        strs.append(s[spaces[n-1]:])\n",
    "            \n",
    "        return \" \".join(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces.sort()\n",
    "        res = ''\n",
    "        j = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if j < len(spaces) and i == spaces[j]:\n",
    "                res += ' '\n",
    "                j += 1\n",
    "            res += c\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res=[]\n",
    "        right=0\n",
    "        for i,ch in enumerate(s):\n",
    "            if right<len(spaces) and spaces[right]==i:\n",
    "                res.append(' ')\n",
    "                right+=1\n",
    "            res.append(ch)\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.ret_list = []\n",
    "        self.s = None\n",
    "\n",
    "    def add_sub_str(self, beg: int, end: int) -> None:\n",
    "        if beg == end == len(self.s):\n",
    "            return\n",
    "        if beg == end == 0:\n",
    "            self.ret_list.append(' ')\n",
    "            return\n",
    "        if end == len(self.s):\n",
    "            self.ret_list.append(self.s[beg: end])\n",
    "            return\n",
    "        self.ret_list.append(self.s[beg: end])\n",
    "        self.ret_list.append(' ')\n",
    "\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        self.s = s\n",
    "        beg = 0\n",
    "        for end in spaces:\n",
    "            self.add_sub_str(beg, end)\n",
    "            beg = end\n",
    "        self.add_sub_str(beg, len(self.s))\n",
    "        return ''.join(self.ret_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        j = 0\n",
    "        for i in spaces:\n",
    "            while j<len(s):\n",
    "                if j == i:\n",
    "                    res+=' '\n",
    "                    j = i\n",
    "                    break\n",
    "                res += s[j]\n",
    "                j+=1\n",
    "        while j<len(s):\n",
    "            res += s[j]\n",
    "            j+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        result = s[:spaces[0]]\n",
    "        for i in range(len(spaces)-1):\n",
    "            result += ' ' + s[spaces[i]:spaces[i+1]]\n",
    "        result += ' ' + s[spaces[len(spaces)-1]:]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = s[:spaces[0]]\n",
    "        for i in range(1, len(spaces)):\n",
    "            ans += \" \" + s[spaces[i - 1]: spaces[i]]\n",
    "        ans += \" \" + s[spaces[-1]:]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        fh = []\n",
    "        x = 0\n",
    "        for i in spaces:\n",
    "            fh.append(s[x:i])\n",
    "            x = i\n",
    "        fh.append(s[x:])\n",
    "        return \" \".join(fh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n=len(s)\n",
    "        arr=[]\n",
    "        \n",
    "        for i,v in enumerate(spaces):\n",
    "            if i==0:\n",
    "                arr.append(s[:v])\n",
    "           \n",
    "               #\n",
    "            if i-1>=0:\n",
    "                arr.append(s[spaces[i-1]:spaces[i]])\n",
    "            if i==len(spaces)-1:\n",
    "                print(v)\n",
    "                arr.append(s[v:])\n",
    "        print(arr)\n",
    "        \n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        a = -1\n",
    "        b = 0\n",
    "        res = []\n",
    "        for i in range(len(s)):\n",
    "            a += 1\n",
    "            if b < len(spaces) and  a == spaces[b]:\n",
    "                res.append(' ')\n",
    "                b += 1\n",
    "            res.append(s[i])\n",
    "        return ''.join(res)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = []\n",
    "        i = 0\n",
    "        for k in spaces:\n",
    "            t = ''\n",
    "            while i < k:\n",
    "                t += s[i]\n",
    "                i += 1\n",
    "            ans.append(t)\n",
    "        ans.append(s[i:])\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "\n",
    "        arr = []\n",
    "        n = len(spaces)\n",
    "\n",
    "        if n == 0:\n",
    "            return s\n",
    "        elif n == 1:\n",
    "            div =  spaces[0]\n",
    "            arr.append(s[ : div])\n",
    "            arr.append(s[div : ])\n",
    "        \n",
    "        else:\n",
    "            arr.append(s[ : spaces[0]])\n",
    "            for i in range(0, n - 1):\n",
    "                left, right = spaces[i], spaces[i + 1]\n",
    "                arr.append(s[left : right])\n",
    "            arr.append(s[spaces[-1] : ])\n",
    "        \n",
    "        # rint(arr)\n",
    "\n",
    "        return \" \".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        idx = 0 \n",
    "        for i in spaces:\n",
    "            ans += s[idx:i]+\" \"\n",
    "            idx = i\n",
    "        return ans+s[idx:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        j = 0\n",
    "        if spaces[0] == 0:\n",
    "            ans = \" \"\n",
    "            j = 1\n",
    "        t = \"\"\n",
    "        for i, x in enumerate(s):\n",
    "            t += x\n",
    "            if j < len(spaces) and i + 1 == spaces[j]:\n",
    "                ans += t + \" \"\n",
    "                t = \"\"\n",
    "                j += 1\n",
    "        return (ans + t) if len(t) > 0 else ans\n",
    "                \n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = s[:spaces[0]]\n",
    "        for i in range(1, len(spaces)):\n",
    "            ans += \" \" + s[spaces[i - 1]:spaces[i]]\n",
    "        return ans + \" \" + s[spaces[-1]:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        j = 0\n",
    "        if spaces[0] == 0:\n",
    "            j = 1\n",
    "            res = ' '\n",
    "        for i in range(len(s)):\n",
    "            res += s[i]\n",
    "            if j<len(spaces) and i == spaces[j]-1:\n",
    "                res += ' '\n",
    "                j += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\r\n",
    "\r\n",
    "        spaces.insert(0, 0)\r\n",
    "        spaces.append(len(s))\r\n",
    "\r\n",
    "        res = []\r\n",
    "        for i in range(len(spaces)-1):\r\n",
    "            #print(s[spaces[i]: spaces[i+1]])\r\n",
    "            res.append(s[spaces[i]: spaces[i+1]])\r\n",
    "\r\n",
    "        return \" \".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "\n",
    "        result=''\n",
    "        j=0\n",
    "        for num in spaces:\n",
    "            result=result+s[j:num]+' '\n",
    "            j=num\n",
    "        result+=s[j:]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n = len(s)\n",
    "        spaces.append(float('inf'))\n",
    "        j = 0 \n",
    "        i = 0\n",
    "        ans = ''\n",
    "        while i < n:\n",
    "            if i < spaces[j]:\n",
    "                ans+=s[i]\n",
    "                i+=1\n",
    "            elif i == spaces[j]:\n",
    "                ans+=' '\n",
    "                j+=1\n",
    "            \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces = [0] + spaces + [len(s)]\n",
    "        ans = ''\n",
    "        for x, y in pairwise(spaces):\n",
    "            ans += s[x: y] + ' '\n",
    "        return ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces = [None] + spaces + [None]\n",
    "        ans = ''\n",
    "        for x, y in pairwise(spaces):\n",
    "            ans += s[x: y] + ' '\n",
    "        return ans.rstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces = [0] + spaces + [len(s)]\n",
    "        ans = ''\n",
    "        for x, y in pairwise(spaces):\n",
    "            ans += s[x: y] + ' '\n",
    "        return ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces = [0] + spaces + [len(s)]\n",
    "        ans = ''\n",
    "        for x, y in pairwise(spaces):\n",
    "            ans += s[x: y] + ' '\n",
    "        return ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        spaces = [0] + spaces + [len(s)]\n",
    "        ans = ''\n",
    "        for x, y in pairwise(spaces):\n",
    "            ans += s[x: y] + ' '\n",
    "        return ans.rstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        idx = 0\n",
    "        tmp = list(s)\n",
    "        for i, k in enumerate(tmp):\n",
    "            if idx < len(spaces) and i == spaces[idx]:\n",
    "                idx += 1\n",
    "                ans += \" \"\n",
    "            ans += k\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        index = 0\n",
    "        lst = list(s)\n",
    "        ans = ''\n",
    "        for i in range(len(lst)):\n",
    "            if index < len(spaces) and i == spaces[index]:\n",
    "                ans += ' '\n",
    "                index += 1\n",
    "            ans += lst[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        idx = 0\n",
    "        tmp = list(s)\n",
    "        for i, k in enumerate(tmp):\n",
    "            if idx < len(spaces) and i == spaces[idx]:\n",
    "                idx += 1\n",
    "                ans += \" \"\n",
    "            ans += k\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        n = len(s)\n",
    "        arr = [0] * n \n",
    "        for x in spaces:\n",
    "            if 0<=x<n:\n",
    "                arr[x] = 1 \n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if arr[i]:\n",
    "                ans.append(' ')\n",
    "            ans.append(s[i])\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        for i in range(len(spaces)):\n",
    "            s = s[:spaces[i] + i] + \" \" + s[spaces[i] + i:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        for i in range(len(spaces)):\n",
    "            s=s[0:spaces[i]+i]+' '+s[spaces[i]+i:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "\n",
    "        for index, value in enumerate(spaces):\n",
    "            s = s[:value] + \" \" + s[value:]\n",
    "            if index < len(spaces) - 1:\n",
    "                spaces[index+1] = spaces[index+1] + index + 1\n",
    "            \n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\r\n",
    "        nums = []\r\n",
    "        for i, x in enumerate(spaces):\r\n",
    "            nums.append(i+x)\r\n",
    "        \r\n",
    "        for num in nums:\r\n",
    "            s = s[:num] + \" \" +  s[num:]\r\n",
    "\r\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        cnt = 0\n",
    "        for i in range(0,len(spaces)):\n",
    "            if i>0:\n",
    "                cnt += 1\n",
    "            s = s[:spaces[i]+cnt:]+' '+s[spaces[i]+cnt::]\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        index = set(spaces)\n",
    "        for i,c in enumerate(s):\n",
    "            if i in index:\n",
    "                ans += (\" \"+c)\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = \"\"\n",
    "        index = set(spaces)\n",
    "        for i,c in enumerate(s):\n",
    "            if i in index:\n",
    "                ans += (\" \"+c)\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: list[int]) -> str:\n",
    "        ans = []\n",
    "        n = len(s)\n",
    "        index = set(spaces)\n",
    "        for i in range(n):\n",
    "            if i in index:\n",
    "                ans.append(\" \")\n",
    "            ans.append(s[i])     \n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        res = ''\n",
    "        spaces = set(spaces)\n",
    "        for i,c in enumerate(s):\n",
    "            if i in spaces:\n",
    "                res += ' '\n",
    "            res += c\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "        ans = []\n",
    "        j = 0\n",
    "        for i in range(len(s)):\n",
    "            if i == spaces[j]:\n",
    "                ans.append(\" \" + s[i])\n",
    "                j += 1\n",
    "                if j == len(spaces):\n",
    "                    ans.append(s[i+1:])\n",
    "                    break\n",
    "            else:\n",
    "                ans.append(s[i])\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "  def addSpaces(self, s: str, spaces: List[int]) -> str:\n",
    "      sl = list(s)\n",
    "      for i in spaces:\n",
    "          sl[i] = \" \" + sl[i]\n",
    "      return \"\".join(sl)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
